extends CharacterBody2D

@export var speed = 50				## 靠近速度
@export var accileration = 300		## 加速度
@export var friction = 100			## 摩擦力,考虑到蝙蝠较轻,摩擦力偏小

@onready var state = $State
@onready var animation = $AnimatedSprite2D
@onready var deathEffect = load("res://Effect/tscn/DeathEffect.tscn")
@onready var hurtEffect = load("res://Effect/tscn/HurtEffect.tscn")
@onready var money = load("res://Props/tscn/Money.tscn")

var direction = Vector2.ZERO

func _ready():
	# 播放动画
	animation.play("fly")
	state.connect("hurt", hurt)
	state.connect("death", death)

'''
	死亡事件
'''
func death():
	# 效果节点
	var effect = deathEffect.instantiate()
	# 设置坐标, 绝对坐标
	effect.global_position = global_position
	# 将动画效果放入世界节点
	get_tree().root.add_child(effect)
	var moneyValue = RandomNumberGenerator.new().randi_range(0, 4)
	
	if moneyValue > 0:
		# 爆金币
		var obj = money.instantiate()
		obj.value = moneyValue
		obj.position = position
		self.get_parent().add_child(obj)
	queue_free()


'''
	受伤事件
'''
func hurt(x):
	var effect = hurtEffect.instantiate()
	effect.global_position = global_position
	# 将动画效果放入世界节点
	get_tree().root.add_child(effect)
